/**
 * 
 */
package com.tvshows.front.view.dialog;

import java.util.List;

import org.apache.log4j.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import com.tvshows.front.controllers.dialog.ActionsController;
import com.tvshows.front.factory.TypeUI;
import com.tvshows.front.listener.dialog.DialogListener;
import com.tvshows.front.ui.dialog.DialogUI;
import com.tvshows.front.view.labels.FontAssociation;
import com.tvshows.front.view.labels.FrontButton;
import com.tvshows.front.view.labels.FrontLabel;
import com.tvshows.front.view.labels.FrontText;
import com.tvshows.structure.bdd.BddQueries;
import com.tvshows.structure.beans.Properties;
import com.tvshows.structure.reader.FileOperations;

/**
 * @author tMidy
 * <p>
 * Options Dialog, configures the application
 * </p>
 */
public class DialogOptionsView
    extends Composite
    implements DialogUI
{

    /**
     * Shell width
     */
    private static final Integer SIZE_WIDTH = 625;

    /**
     * Shell Height
     */
    private static final Integer SIZE_HEIGHT = 450;

    /**
     * width of the tree
     */
    private static final Integer SIZE_TREE = 200;

    /**
     * The options tree
     */
    private Tree optionsTree;

    /**
     * The software text field
     */
    private FrontText softwarePathField;

    /**
     * Browse button
     */
    private FrontButton browseButton;

    /**
     * The apply, cancel buttons
     */
    private FrontButton applyButton, cancelButton;

    /**
     * The properties item
     */
    private TreeItem propertiesTreeItem;

    /**
     * The about item
     */
    private TreeItem aboutTreeItem;

    /**
     * the {@link FileOperations}
     */
    private FileOperations op;

    /**
     * The listener
     */
    private Listener listener;

    /**
     * The groups
     */
    private Group softwarePathGroup, confirmationGroup;

    /**
     * THe changesList
     */
    private Properties changesList;

    /**
     * The browse button for hibernate conf
     */
    private FrontButton browseButtonHibernate;

    /**
     * the About group
     */
    private Group aboutGroup;

    /**
     * The properties field
     */
    private FrontText softwarePropertiesField;

    /**
     * The browse button for properties
     */
    private FrontButton browsePropertiesButton;

    /**
     * Constructeur
     * 
     * @param parent parent composite
     * @param style composite style
     */
    public DialogOptionsView( Composite parent, int style )
    {
        super( parent, style );
        setSize( SIZE_WIDTH, SIZE_HEIGHT );
        initComponents();
    }

    /**
     * Init Components
     */
    private void initComponents()
    {
        changesList = new Properties();
        listener = new DialogListener( this );
        initTree();

    }

    /**
     * Initilializes the tree in the dialog
     */
    private void initTree()
    {
        optionsTree = new Tree( this, SWT.NONE );
        optionsTree.setSize( SIZE_TREE, this.getShell().getBounds().height );
        optionsTree.addListener( SWT.Selection, listener );

        propertiesTreeItem = new TreeItem( optionsTree, SWT.NONE );
        propertiesTreeItem.setText( "Propriétés de l'application" );

        aboutTreeItem = new TreeItem( optionsTree, SWT.NONE );
        aboutTreeItem.setText( "A propos..." );
    }

    /**
     * Set the corresponding panel to the item passed in parameter
     * 
     * @param itemName the clicked item
     */
    public void initOptionsPanel( String itemName )
    {

        if ( null == itemName )
        {
            initAboutPanel();
        }
        else
        {
            initPropertiesPanel();
        }
        if ( null == confirmationGroup )
        {
            initConfirmationGroup();
        }
    }

    /**
     * Initializes the about Panel
     */
    private void initAboutPanel()
    {
        if ( null != softwarePathGroup )
        {
            disposeSoftwareConfPanel();
        }

        aboutGroup = new Group( this, SWT.BORDER_SOLID );
        aboutGroup.setBounds( 210, 0, 400, 290 );
        aboutGroup.setText( "A propos..." );

        /*
         * The about label
         */
        FrontLabel aboutLabel = new FrontLabel( aboutGroup, SWT.NONE );
        aboutLabel.setBounds( 10, 50, 350, 200 );
        aboutLabel.setText( FontAssociation.ABOUT_TEXT );

        cancelButton.setVisible( false );
        applyButton.setText( "Ok" );

    }

    /**
     * Removes all the components from previous screen
     */
    private void disposeSoftwareConfPanel()
    {
        softwarePathGroup.dispose();
    }

    /**
     * initializes the properties panel
     */
    private void initPropertiesPanel()
    {
        if ( null != aboutGroup )
        {
            aboutGroup.dispose();
            cancelButton.setVisible( true );
            applyButton.setText( "Appliquer les modifications" );
        }
        softwarePathGroup = new Group( this, SWT.BORDER_SOLID );
        softwarePathGroup.setBounds( 210, 0, 400, 290 );
        softwarePathGroup.setText( "Configuration du logiciel" );

        /*
         * Configuration du fichier de properties
         */
        /*
         * The torrent software field
         */
        FrontLabel softwarePropertiesLabel = new FrontLabel( softwarePathGroup, SWT.NONE );
        softwarePropertiesLabel.setText( "Chemin vers le fichier de propriétés :" );
        softwarePropertiesLabel.setBounds( 10, 25, 250, 20 );

        softwarePropertiesField = new FrontText( softwarePathGroup, SWT.BORDER );
        softwarePropertiesField.setBounds( 10, 45, 200, 25 );
        softwarePropertiesField.setEditable( false );

        browsePropertiesButton = new FrontButton( softwarePathGroup, SWT.BORDER_DASH );
        browsePropertiesButton.setText( "Parcourir..." );
        browsePropertiesButton.setBounds( 250, 45, 100, 25 );
        browsePropertiesButton.addListener( SWT.Selection, listener );

        /*
         * Configuration software torrent
         */

        /*
         * The softwarePath Label
         */
        FrontLabel softwarePathLabel = new FrontLabel( softwarePathGroup, SWT.NONE );
        softwarePathLabel.setText( "Chemin vers le soft torrent :" );
        softwarePathLabel.setBounds( 10, 75, 150, 20 );

        softwarePathField = new FrontText( softwarePathGroup, SWT.BORDER );
        softwarePathField.setBounds( 10, 95, 200, 25 );
        softwarePathField.setEditable( false );

        browseButton = new FrontButton( softwarePathGroup, SWT.BORDER_DASH );
        browseButton.setText( "Parcourir..." );
        browseButton.setBounds( 250, 95, 100, 25 );
        browseButton.addListener( SWT.Selection, listener );

        op = FileOperations.getInstance();
        List<String> properties = op.readProperties();

        updateProperties( properties );

        this.layout();

    }

    /**
     * Updates the properties
     * 
     * @param properties the properties to set
     */
    public void updateProperties( List<String> properties )
    {
        if ( !properties.isEmpty() )
        {
            softwarePathField.setText( properties.get( 0 ) );
        }
        else
        {
            softwarePathField.setText( "" );
        }
        softwarePropertiesField.setText( op.getPropertyPath() );
    }

    /**
     * Initializes Ok and cancel button
     */
    private void initConfirmationGroup()
    {
        confirmationGroup = new Group( this, SWT.BORDER_SOLID );
        confirmationGroup.setBounds( 210, 300, 400, 90 );
        confirmationGroup.setText( "Confirmation" );

        applyButton = new FrontButton( confirmationGroup, SWT.NONE );
        applyButton.setText( "Appliquer les modifications" );
        applyButton.setBounds( 20, 20, 170, 25 );
        applyButton.addListener( SWT.Selection, listener );

        cancelButton = new FrontButton( confirmationGroup, SWT.NONE );
        cancelButton.setBounds( 20 + 200, 20, 100, 25 );
        cancelButton.setText( "Annuler" );
        cancelButton.addListener( SWT.Selection, listener );
    }

    /**
     * @return the propertiesTreeItem
     */
    public TreeItem getPropertiesTreeItem()
    {
        return propertiesTreeItem;
    }

    public TreeItem getAboutTreeItem()
    {
        return aboutTreeItem;
    }

    /**
     * @return the optionsTree
     */
    public Tree getOptionsTree()
    {
        return optionsTree;
    }

    /**
     * @return the browseButton
     */
    public Button getBrowseButton()
    {
        return browseButton;
    }

    public FrontButton getBrowseButtonHibernate()
    {
        return browseButtonHibernate;
    }

    public FrontButton getBrowsePropertiesButton()
    {
        return browsePropertiesButton;
    }

    /**
     * @return the applyButton
     */
    public Button getApplyButton()
    {
        return applyButton;
    }

    /**
     * @return the cancelButton
     */
    public Button getCancelButton()
    {
        return cancelButton;
    }

    /**
     * Updates the configuration File
     */
    public void applyChanges()
    {
        if ( !softwarePathGroup.isDisposed()
            && ( !( "".equals( softwarePropertiesField.getText() ) ) || !( "".equals( softwarePathField.getText() ) ) ) )
        {
            changesList.setSoftwarePath( softwarePathField.getText() );
            try
            {
                BddQueries.getBddQueries().updatePropertyPath( softwarePropertiesField.getText() );
                op.changeFile( softwarePropertiesField.getText() );
                op.writeProperties( changesList );
            }
            catch ( Exception e )
            {
                Logger.getLogger( DialogOptionsView.class ).error(
                    "Problème de configuration majeur :" + e.getMessage() );

                ActionsController.popUpConfigurationProblem( this );

            }
        }
    }

    /**
     * @param path the new Path the soft
     */
    public void setSoftPath( String path )
    {
        softwarePathField.setText( path );
        changesList.setSoftwarePath( path );
    }

    /**
     * @param path the new property path to set
     */
    public void setPropertiesPath( String path )
    {
        softwarePropertiesField.setText( path );
    }

    /**
     * @see com.tvshows.front.ui.dialog.DialogUI#getType()
     */
    @Override
    public TypeUI getType()
    {
        return TypeUI.OPTION_VIEW;
    }

    @Override
    public void initFormData()
    {

    }

    @Override
    public void initComposites()
    {

    }
}
